package com.uxwine.alarm;

import java.util.Calendar;

public class TimeRecord
{
	public static final int YEAR 	= 0;
	public static final int MONTH	= 1;
	public static final int DATE	= 2;
	public static final int HOUR	= 3;
	public static final int MINUTE	= 4;
	public static final int SECOND	= 5;
	public static final int MLLSEC	= 6;
	public static final int YMONTH	= 7;
	public static final int YDATE	= 8;
	
	private int[] mData = new int[10];
	
	public TimeRecord()
	{
		reset();
	}
	
	public TimeRecord(long time)
	{
		set(time);
	}
	
	public TimeRecord(String time)
	{
		from(time);
	}
	
	public TimeRecord(int[] values)
	{
		if(values == null || values.length != 10)
			throw new IllegalArgumentException();
		
		mData = values;
	}
	
	@Override
	public String toString()
	{
		StringBuffer sb = new StringBuffer();
		for(int i = 0; i < 10; i++)
		{
			if(i == 0)
				sb.append(mData[i]);
			else
				sb.append(":").append(mData[i]);
		}		
		return sb.toString();
	}
	
	public void from(String time)
	{
		if(null == time)
			reset();
		String[] vs = time.split(":");
		if(vs.length != 10)
			throw new IllegalArgumentException();
		for(int i = 0; i < 10; i++)
		{
			mData[i] = Integer.parseInt(vs[i]);
		}
	}
	
	public void add(int key, int add)
	{
		if(key < 0 || key > 10)
			throw new IllegalArgumentException();
		mData[key] += add;
	}
	
	public int get(int key)
	{
		if(key < 0 || key > 10)
			throw new IllegalArgumentException();
		return mData[key];
	}
	
	public int[] getValues()
	{
		return mData;
	}
	
	public void reset()
	{
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(System.currentTimeMillis());
		set(c);
	}
	
	public void set(long time)
	{
		final Calendar c = Calendar.getInstance();
		c.setTimeInMillis(time);
		set(c);
	}
	
	public void set(final Calendar c)
	{
		set(c.get(Calendar.YEAR),
			c.get(Calendar.MONTH),
			c.get(Calendar.DAY_OF_MONTH),
			c.get(Calendar.HOUR_OF_DAY),
			c.get(Calendar.MINUTE), 0, 0, 0, 0);
	}
	
	public void set(int key, int value)
	{
		if(key < 0 || key > 10)
			throw new IllegalArgumentException();
		mData[key] = value;
	}
	
	public void setYTime(int yyear, int ymonth)
	{
		set(0, 0, 0, 0, 0, 0, 0, yyear, ymonth);
	}
	
	public void setTime(int hour, int min)
	{
		setTime(0, 0, 0, hour, min);
	}
	
	public void setTime(int day, int hour, int min)
	{
		set(0, 0, day, hour, min, 0, 0, 0, 0);
	}
	
	public void setTime(int month, int day, int hour, int min)
	{
		set(0, month, day, hour, min, 0, 0, 0, 0);
	}
	
	public void setTime(int year, int month, int day, int hour, int min)
	{
		set(year, month, day, hour, min, 0, 0, 0, 0);
	}
	
	public void set(int year, int month, int day, int hour, int min, int sec, int msec, int yyear, int ymonth)
	{
		mData[0] = year;
		mData[1] = month;
		mData[2] = day;
		mData[3] = hour;
		mData[4] = min;
		mData[5] = sec;
		mData[6] = msec;
		mData[7] = yyear;
		mData[8] = ymonth;
		mData[9] = 0;
	}
}
